//
//  ST_AudioPlayer.m
//  ShiTing
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 liudianliang. All rights reserved.
//

#import "ST_AudioPlayer.h"
#import <AVFoundation/AVFoundation.h>

@interface ST_AudioPlayer()
@property (nonatomic, retain) AVPlayer *musicPlayer;

@property (nonatomic, retain) NSTimer *timer;

@end

@implementation ST_AudioPlayer


- (id)init
{
    self = [super init];
    if (self) {
       _musicPlayer = [[AVPlayer alloc] init];
        // 系统已经注册好了一个音乐结束时发送的通知中心 所以 直接对这个通知 addobserver
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MusicDidFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}
- (void)setUrlStr:(NSString *)urlStr
{
    if (_urlStr != urlStr) {
        [_urlStr release];
        _urlStr = [urlStr copy];
    }
    NSURL *url = [NSURL URLWithString:_urlStr];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
    [self.musicPlayer replaceCurrentItemWithPlayerItem:playerItem];
//    [playerItem release];
    [self play1];
 
//    [self pause1];
}

- (void)play1
{
    [self.musicPlayer play];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(avplayerPlay)]) {
//        [self.delegate avplayerPlay];
//    }
    
    
    [self creatTimer];
}

- (void)pause1
{
    [self.musicPlayer pause];
    if (self.delegate && [self.delegate respondsToSelector:@selector(avplayerPause)]) {
        [self.delegate avplayerPause];
    }
    [self.timer invalidate];
    self.timer = nil;
}

//- (void)stop1
//{
//    [self pause1];
//    [self.timer invalidate];
//    self.timer = nil;
//    if (self.delegate && [self.delegate respondsToSelector:@selector(avplayerStop)]) {
//        [self.delegate avplayerStop];
//    }
//}

+ (instancetype)sharedAudio
{
    static ST_AudioPlayer *player = nil;
    static dispatch_once_t  onceToken;
    dispatch_once(&onceToken, ^{
        player = [[ST_AudioPlayer alloc] init];
    });
    return player;
    
}

- (void)creatTimer
{
    if (self.timer == nil) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        [self.timer fire];
    }
}

- (void)timerAction
{
    float progress = (float)self.musicPlayer.currentTime.value / (float)self.musicPlayer.currentTime.timescale;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(avplayerChangeCurrentTime:)] ) {
        [self.delegate avplayerChangeCurrentTime:progress];
    }
    
}

// 跳转到某个时间
- (void)seekToTime:(float)progress
{
    [self pause1];
    
    [self.musicPlayer seekToTime:CMTimeMakeWithSeconds(progress, self.musicPlayer.currentTime.timescale) completionHandler:^(BOOL finished) {
        if (finished) {
            [self play1];
        }
    }];
    
}

- (void)MusicDidFinish
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(avplayerDidFinish)]) {
        [self.delegate avplayerDidFinish];
    }
}


@end
